package com.zhang.demo.ui.adaper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.collection.ArrayMap;
import androidx.recyclerview.widget.RecyclerView;

import com.zhang.demo.injector.ViewType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangke on 16/10/17.
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> implements
    OnItemClickListener, OnItemLongClickListener {

    protected final String myTag = this.getClass().getSimpleName();
    protected final List<T> dataList;
    protected final Context context;
    protected Map<Class<?>, Integer> itemTypeClassMap;

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
        this.dataList = new ArrayList<>();
        initItemViewType();
    }

    public void clearData() {
        dataList.clear();
    }

    public void setData(List<T> data) {
        dataList.clear();
        if (data != null) {
            dataList.addAll(data);
        }
    }

    public void addData(List<T> data) {
        if (data != null) {
            dataList.addAll(data);
        }
    }

    public void addData(T data) {
        if (data != null) {
            dataList.add(data);
        }
    }

    public void removeData(int position) {
        if (position >= 0 && position < dataList.size()) {
            dataList.remove(position);
        }
    }

    public int removeData(T data) {
        if (data != null) {
            int position = -1;
            for (int i = 0; i < dataList.size(); i++) {
                if (data.equals(dataList.get(i))) {
                    position = i;
                    break;
                }
            }
            if (position >= 0) {
                dataList.remove(position);
                return position;
            }
        }
        return 0;
    }

    public void setData(int position, T data) {
        dataList.set(position, data);
    }

    public List<T> getData() {
        return dataList;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder holder = createHolder(parent, viewType);
        holder.setItemClickListener(this);
        holder.setItemLongClickListener(this);
        return holder;
    }

    public abstract BaseViewHolder createHolder(ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.onBind(getItem(position));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        holder.onPayloadsChanged(payloads);
    }

    public Object getItem(int position) {
        return dataList.get(position);
    }

    protected void initItemViewType() {

    }

    protected void bindItemViewType(@ViewType int viewType, Class<?> clz) {
        if (itemTypeClassMap == null) {
            itemTypeClassMap = new ArrayMap<>();
        }
        itemTypeClassMap.put(clz, viewType);
    }

    @ViewType
    protected int getCustomViewType(Object data) {
        return ViewType.VIEW_TYPE_NONE;
    }

    @Override
    public int getItemViewType(int position) {
        if (itemTypeClassMap == null) {
            return super.getItemViewType(position);
        } else {
            Object data = getItem(position);
            Integer viewType = itemTypeClassMap.get(data.getClass());
            if (viewType == null) {
                return getCustomViewType(data);
            }
            return viewType;
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener itemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        itemClickListener = listener;
    }

    @Override
    public void onItemClick(BaseViewHolder holder, View targetView, int position) {
        if (itemClickListener != null) {
            itemClickListener.onItemClick(holder, targetView, position);
        }
    }

    @Override
    public boolean onItemLongClick(BaseViewHolder holder, View targetView, int position) {
        if (itemLongClickListener != null) {
            return itemLongClickListener.onItemLongClick(holder, targetView, position);
        }
        return false;
    }

    private OnItemLongClickListener itemLongClickListener;
    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
        itemLongClickListener = longClickListener;
    }

    public Context getContext() {
        return context;
    }

    public String getString(@StringRes int resId) {
        return getContext().getString(resId);
    }

    public final String getString(@StringRes int resId, Object... formatArgs) {
        return getContext().getString(resId, formatArgs);
    }
}
